package logic

import (
	"context"
	"fmt"
	"shuqi_novel/models"

	"shuqi_novel/shop_rpc/internal/svc"
	"shuqi_novel/shop_rpc/shop_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookUpdateLogic {
	return &BookUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookUpdateLogic) BookUpdate(in *shop_rpc.BookUpdateRequest) (*shop_rpc.BookUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var book models.Book
	if models.DB.Where("id = ?", in.BookId).Limit(1).Find(&book).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	if book.ID == 0 {
		return nil, fmt.Errorf("小说不存在")
	}
	if book.AuthorId != int(in.AuthorId) {
		return nil, fmt.Errorf("你不是小说的所有者，无权修改")
	}
	book.Des = in.Des
	book.Type = in.Type
	book.Title = in.Title
	book.Image = in.Image
	if models.DB.Where("id = ?", in.BookId).Updates(&book).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &shop_rpc.BookUpdateResponse{}, nil
}
